package com.straw.jdk.algorithm.loadblance;

import com.straw.hat.common.util.RandomNumGenerator;

import java.util.*;

/**
 * @auth:yangbb
 * @date: 2019/11/11
 * @time: 0:03
 * @company: 海贼王
 */
public class WeightRandom {

    private static final Map<String,Integer> WEIGHT_MAP = new HashMap();

    private static int totalWeight ;


    static {
        WEIGHT_MAP.put("192.168.0.1",21);
        WEIGHT_MAP.put("192.168.0.2",19);
        WEIGHT_MAP.put("192.168.0.3",3);
        WEIGHT_MAP.put("192.168.0.4",37);
        WEIGHT_MAP.put("192.168.0.5",13);
        WEIGHT_MAP.put("192.168.0.6",27);
        WEIGHT_MAP.put("192.168.0.7",30);
        WEIGHT_MAP.put("192.168.0.8",10);
        WEIGHT_MAP.put("192.168.0.9",8);
        WEIGHT_MAP.put("192.168.0.10",42);

        for (Integer weightValue : WEIGHT_MAP.values()) {
            totalWeight += weightValue;
        }
    }

    public static String getServer(){
        String server = null;
        int randomNum = RandomNumGenerator.generator(1, totalWeight);
        for (Map.Entry<String, Integer> entry : WEIGHT_MAP.entrySet()) {
            randomNum = entry.getValue() - randomNum;
            if (randomNum <= 0) {
                server = entry.getKey();
            }
        }
        return server;
    }





}
